const app = getApp()
let host = app.globalData.host;
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
  },
 
  //表单提交
  formSubmit(e){

    let {cameraAppKey,cameraSecret} = e.detail.value
    wx.request({
      url: 'https://open.ys7.com/api/lapp/token/get',
      data: {
        appKey: cameraAppKey,
        appSecret: cameraSecret
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },success (res) {
        console.log(res.data)
        let cameraAccessToken = res.data.data.accessToken
        let cameraTokenTime = res.data.data.expireTime
        wx.request({
          url: host+'/updateCamera',
          data: {
            cameraId: 1,
            cameraAppKey: cameraAppKey,
            cameraSecret: cameraSecret,
            cameraAccessToken: cameraAccessToken,
            cameraTokenTime: cameraTokenTime
          },
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },success (res) {
            console.log(res.data)
          },fail (error){
            console.log("失败")
          }
        })
      },
      fail (error){
        console.log("失败")
      }
    })
    wx.redirectTo({
      url: '../cameras/cameras'
    })

  },

  onLoad: function (options) {
    let that = this
    wx.request({
      url: host + '/showCamera?cameraId=1',
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
       },success (res) {
         that.setData({
            cameraAppKey: res.data.cameraAppKey,
            cameraSecret: res.data.cameraSecret,
            cameraAccessToken: res.data.cameraAccessToken,
            cameraTokenTime: new Date(parseInt(res.data.cameraTokenTime)) + ""
         })
       },
      fail (error){
       console.log("失败")
      }
    })
  }
})

